import { Body, Controller, Get, Post, Req } from '@nestjs/common';
import { AppPaymentOrderService } from './payment-order.service';
// import { PaymentOrderListDto } from './dto/list.dto';
// import { EditPaymentOrderDto } from './dto/edit.dto';
import { AppleRestoreSubscribeDto, AppleSubscribeDto } from './dto/add.dto';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { IosAppPayService } from './ios-app-pay.service';
import { ApplePayPushDto } from './dto/apple-pay-push.dto';
import { FileService } from 'src/modules/system/common/upload/file.service';
import { ConfigService } from '@nestjs/config';
// import { ApiRes } from 'src/common/decorator/api-res.decorator';
// import { PaymentOrder } from './payment-order.entity';
// import { PaymentOrderQueryDto } from './dto/query.dto';
// import { PaymentOrderModel } from './feedback.model';

@ApiTags('app支付订单模块')
@ApiBearerAuth()
@Controller('app/payment-order')
export class AppPaymentOrderController {
  constructor(
    private readonly appPaymentOrderService: AppPaymentOrderService,
    private readonly iosAppPayService: IosAppPayService,
    private readonly fileService: FileService,
    private readonly configService: ConfigService,
  ) {}

  // @Post('/list')
  // @ApiRes(PaymentOrderModel, true)
  // async findAll(@Body() params: PaymentOrderListDto) {
  //   return {
  //     data: await this.appPaymentOrderService.findAll(params),
  //   };
  // }

  // @Get('/query')
  // @ApiRes(PaymentOrderModel)
  // async query(@Query() params: PaymentOrderQueryDto) {
  //   return {
  //     data: await this.appPaymentOrderService.getUserDetail(params.id),
  //   };
  // }

  // @Post('/edit')
  // @ApiRes(PaymentOrder)
  // async edit(@Body() user: EditPaymentOrderDto) {
  //   return {
  //     data: await this.appPaymentOrderService.update(user),
  //   };
  // }

  @Post('/apple/subscribe')
  @ApiOperation({
    summary: '订阅',
  })
  async add(@Body() data: AppleSubscribeDto, @Req() req) {
    // if (this.configService.get('NODE_ENV') === 'production') {
    //   this.fileService.createAndWriteJsonFile(
    //     `subscribe-${dateFormat(new Date())}.json`,
    //     data,
    //   );
    // }
    const { uid } = req.auth;
    const orderRes = await this.iosAppPayService.subscribe(uid, data);
    if (!orderRes.originalTransactionId) {
      return {
        code: '100000',
        message: '订阅失败',
      };
    }
    return {
      data: orderRes,
    };
    // return {
    //   data: await this.appPaymentOrderService.create(data),
    // };
  }

  @Post('/apple/paySystemPush')
  @ApiOperation({
    summary: '苹果通知',
  })
  async paySystemPush(@Body() data: ApplePayPushDto) {
    return {
      data: await this.iosAppPayService.paySystemPush(data.signedPayload),
    };
  }

  @Post('/apple/restoreSubscription')
  @ApiOperation({
    summary: '恢复订阅',
  })
  async restoreSubscription(
    @Body() data: AppleRestoreSubscribeDto,
    @Req() req,
  ) {
    const { uid } = req.auth;
    try {
      const orderRes = await this.iosAppPayService.restoreSubscription(
        uid,
        data,
      );
      return {
        data: orderRes,
      };
    } catch (error) {
      return {
        code: '100000',
        message: '订单不存在或已过期',
      };
    }
  }
  // @Post('/add')
  // @ApiOperation({
  //   summary: '创建订单',
  // })
  // @ApiRes(PaymentOrder)
  // async create(@Body() data: CreatePaymentOrderDto) {
  //   console.log('创建订单', data);
  //   return {
  //     data: await this.appPaymentOrderService.create(data),
  //   };
  // }

  @Get('/apple/subscribeTest')
  @ApiOperation({
    summary: '测试订阅回调',
  })
  Test() {
    this.iosAppPayService.test();
    return {
      data: '',
    };
  }
}
